Skip to content

Conversation

@mbrandonw
Copy link
Member

There's a "quirk" (maybe bug?) in CloudKit where saving a child record will cause iCloud to update the parent's recordChangeTag (this part is documented), but that update does not get sent to the CKSyncEngine (not documented, possible bug). This can cause us to have a stale lastKnownServerRecord, which can cause updates to the parent record to fail. Most of the time that is handled just fine by the conflict resolution mechanism in the library, but when sharing in particular we have to handle the situation more delicately. So now we make sure to fetch the freshest record from iCloud before sharing.

@mbrandonw mbrandonw requested a review from stephencelis January 9, 2026 00:13
Comment on lines +193 to +203
// NB: "Touch" parent records when saving a child:
if let parent = recordToSave.parent,
// If the parent isn't also being saved in this batch.
!recordsToSave.contains(where: { $0.recordID == parent.recordID }),
// And if the parent is in the database.
let parentRecord = storage[parent.recordID.zoneID]?.records[parent.recordID]?.copy()
as? CKRecord
{
parentRecord._recordChangeTag = UUID().uuidString
storage[parent.recordID.zoneID]?.records[parent.recordID] = parentRecord
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes the mock cloud database behave more like CloudKit. And this change alone made some tests fail.

* Create parent record and synchronize.
* Create child record and synchronize.
* Share parent record.
* See: https://github.com/pointfreeco/sqlite-data/pull/363
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we start using @Test(.bug("<url>")) to track this kind of thing?

@mbrandonw mbrandonw merged commit e64d353 into main Jan 13, 2026
5 checks passed
@mbrandonw mbrandonw deleted the fix-share-child-parent branch January 13, 2026 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants